package com.blueocean_health.care.management.controller;

import com.blueocean_health.care.management.common.base.em.TimeType;
import com.blueocean_health.care.management.common.base.time.TimeUtils;
import com.blueocean_health.care.management.common.excel.ExcelUtil;
import com.blueocean_health.care.management.domain.base.SysResult;
import com.blueocean_health.care.management.domain.dto.SelectCareSalaryDto;
import com.blueocean_health.care.management.domain.dto.SelectCareSalarySummaryDetailRespDto;
import com.blueocean_health.care.management.domain.dto.SelectCareSalarySummaryRespDto;
import com.blueocean_health.care.management.transfer.client.SelectCareSalaryClientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.BufferedOutputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

/**
 * 护工工资报表
 * @author cp
 */
@RestController
@RequestMapping(value = "nurse/salary/")
public class SelectCareSalaryController {
	
	@Autowired
	private SelectCareSalaryClientService selectCareSalaryClientService;
	


	/**
	 * 提成汇总表
	 * @param careSalaryDto
	 * @return
	 * @throws
	 */
	@RequestMapping(value = "summary")
	public SysResult queryNurseSalarySummary(@RequestBody SelectCareSalaryDto careSalaryDto) {
		return selectCareSalaryClientService.queryNurseSalarySummary(careSalaryDto);
	}


	/**
	 * 提成汇总表详情
	 * @param careSalaryDto
	 * @return
	 * @throws
	 */
	@RequestMapping(value = "summary/detail")
	public SysResult queryNurseSalarySummaryDetail(@RequestBody SelectCareSalaryDto careSalaryDto) {
		return selectCareSalaryClientService.queryNurseSalarySummaryDetail(careSalaryDto);
	}




    /**
     * 提成汇总表导出Excel
     * @param careSalaryDto
     * @return
     * @throws
     */
    @RequestMapping(value = "summary/excel")
    public void queryNurseSalarySummaryExcel(SelectCareSalaryDto careSalaryDto, HttpServletResponse response) throws Exception{
        List<SelectCareSalarySummaryRespDto> respDtos = selectCareSalaryClientService.queryNurseSalarySummaryExcel(careSalaryDto);
        String finalFileName = null;
		if ( respDtos == null || respDtos.size() == 0 ){
			respDtos = new ArrayList<>();
		}
        try {
            finalFileName = URLEncoder.encode("提成汇总表_"+ TimeUtils.getTime(TimeType.yyyyMMddHHmmssSSS)+".xls", "UTF8");
        } catch (Exception e) {
            finalFileName  = "care_salary_summary_"+TimeUtils.getTime(TimeType.yyyyMMddHHmmssSSS)+".xls";
        }
        response.addHeader("Content-Disposition", "attachment;filename=" + finalFileName);
        OutputStream toClient = new BufferedOutputStream(response.getOutputStream());

        ExcelUtil.getInstance().exportObj2Excel(toClient, respDtos, SelectCareSalarySummaryRespDto.class);
    }


    /**
     * 提成汇总明细导出Excel
     * @param careSalaryDto
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "summary/detail/excel")
    public void queryNurseSummaryDetailExcel(SelectCareSalaryDto careSalaryDto, HttpServletResponse response) throws Exception{
        List<SelectCareSalarySummaryDetailRespDto> list = selectCareSalaryClientService.queryNurseSummaryDetailExcel(careSalaryDto);
        List<SelectCareSalarySummaryDetailRespDto> datalist = new ArrayList<>();
        list.forEach(dto ->{
			SelectCareSalarySummaryDetailRespDto respDto = new SelectCareSalarySummaryDetailRespDto();
			respDto.setDayPay(dto.getDayPay());
			respDto.setOrderId(dto.getOrderId());
			respDto.setOutworkerName(dto.getOutworkerName());
			respDto.setPatientName(dto.getPatientName());
			respDto.setWorkingDay(dto.getWorkingDay());
			respDto.setWorkName(dto.getWorkName());
			respDto.setDeptName(dto.getDeptName());
			respDto.setReceiptNumber(dto.getReceiptNumber());
			respDto.setOrderTypeName(dto.getOrderTypeName());
			datalist.add(respDto);
		});

        String finalFileName = null;
        try {
            finalFileName = URLEncoder.encode("汇总详情表_"+TimeUtils.getTime(TimeType.yyyyMMddHHmmssSSS)+".xls", "UTF8");
        } catch (Exception e) {
            finalFileName  = "care_salary_summarydetail_"+TimeUtils.getTime(TimeType.yyyyMMddHHmmssSSS)+".xls";
        }
        response.addHeader("Content-Disposition", "attachment;filename=" + finalFileName);
        OutputStream toClient = new BufferedOutputStream(response.getOutputStream());

        ExcelUtil.getInstance().exportObj2Excel(toClient, datalist, SelectCareSalarySummaryDetailRespDto.class);
    }
	
	
	
}
